<?php
/**
 * @version 1.0 上传逻辑类
 * @internal
 * @link
 * @property
 * @see
 * @since
 * @uses
 *
 */
namespace app\user\logic;

use app\Api;
use app\model\UserExpressLog as ModelUserExpressLog;
use app\model\Area as ModelArea;
use app\model\UserQuestion as ModelUserQuestion;

class User extends Api
{
    private static $obj = null;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 类入口
     * @return User|null
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * h
     * @param $arr
     * @param $str
     * @return array
     */
    public function getNewArray($arr, $str)
    {
        if (empty($arr) || empty($str)) return [];

        $new_arr = [];
        foreach ($arr as $value) {
            $new_value = $value;
            unset($new_value[$str]);
            $new_arr[$value[$str]][] = $new_value;
        }

        return $new_arr;
    }

    /**
     * 获取商品列表
     * @param $arr_where
     * @param $str_field
     * @return array
     */
    public function getQuestionList($arr_where, $str_field, $str_key)
    {
        $question_list = ModelUserQuestion::getInstance()->toSelect($arr_where, $str_field);
        return $this->getNewArray($question_list, $str_key);
    }

    /**
     * 添加快递查询历史记录
     * @param $int_user_id int 用户id
     * @param $int_express_id int 快递公司id
     * @param $str_courier_number string 查询单号
     * @return bool
     */
    public function addExpressLog($int_user_id, $int_express_id, $str_courier_number)
    {
        //历史id
        $log_id = ModelUserExpressLog::getInstance()->toValue(['user_id' => $int_user_id, 'express_id' => $int_express_id, 'courier_number' => $str_courier_number], 'log_id');
        if ($log_id > 0) return true;

        //添加历史记录
        $res = ModelUserExpressLog::getInstance()->toAdd(['user_id' => $int_user_id, 'express_id' => $int_express_id, 'courier_number' => $str_courier_number]);
        if ($res) return true;
        return false;
    }

    /**
     * 验证地址id
     * @param $int_area_id int 地址id
     * @param int $int_parent_id int 上级地址id
     * @return string
     */
    public function checkAreaId($int_area_id, $int_parent_id = 0)
    {

        if (!($int_area_id > 0)) return '';

        return ModelArea::getInstance()->getAreaName($int_area_id, $int_parent_id);
    }

}
